<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <h1>ajax代替地址栏请求页面</h1>
    <button id="btn"> ajax实现局部刷新 </button>
    <ul id="oul">
        <!-- <li>姓名：xx,密码：yy</li>
        <li>姓名：xx,密码：yy</li>
        <li>姓名：xx,密码：yy</li> -->
    </ul>
</body>

</html>
<script>
    
    btn.onclick = function () {


        
        let xhr = new XMLHttpRequest()
        xhr.open("get", "./data/persons.json")
        xhr.send()
        xhr.onreadystatechange = function () {

            if (xhr.readyState == 4 && xhr.status == 200) {
                // 接收结果：xhr.responseText 结果会以文本的形式呈现
                // console.log(xhr.responseText, 55555);
                // console.log(JSON.parse(xhr.responseText));

                let persons=JSON.parse(xhr.responseText)
                // console.log(persons);



                let str=``
                persons.forEach(item => {
                    str+=`<li>姓名：${item.username},密码：${item.password}</li>`
                });
                // console.log(str);
                oul.innerHTML=str

            }
        }
    }






</script>